﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using XCT.Class;
using XCT.Functions;

namespace XCT.Forms
{
    public partial class FormLapKH_EDC : DevExpress.XtraEditors.XtraForm
    {
        public FormLapKH_EDC()
        {
            InitializeComponent();
        }

        private FormMain formMain;
        public FormLapKH_EDC(FormMain formMain)
        {
            InitializeComponent();
            this.formMain = formMain;
        }

        #region "Biến"

        clsReports clsRp = new clsReports();

        string TuanKH;
        string pSolsx="";
        string pMaCT ="";
        string pMaMau="";
        string pCoSo ="";
        string strduongdan = clsDungChung.strduongdan;
        string strbaocao = "Rpt_PhieuTK_EDC.rpt";
        int flag = 0;

        #endregion

        #region "Hàm"
        // đưa dữ liệu vào combo tuần tháng năm
        private void fKhoiTaoComBo()
        {
            for (int i = 2010; i <= DateTime.Now.Year + 1; i++)
                cmbNamKH.Properties.Items.Add(i.ToString());
            cmbNamKH.Text = DateTime.Now.Year.ToString();
            cmbThangKH.SelectedIndex = DateTime.Now.Month - 1;
        }

        // đưa dữ liệu vào lstbox lsx
        private void fDocLSXTheoTuanKH_BC_EDC(string TuanKH)
        {
            DataSet ds = new DataSet();
            ds = clsRp.DocLSXTheoTuanKH_BC_EDC(TuanKH);
            chListLSX.DataSource = ds.Tables[0].DefaultView;
            chListLSX.DisplayMember = "SoLSX";
            chListLSX.ValueMember = "SoLSX";
        }

         //đưa dữ liêu vào listbox Mau san pham
        private void fDocMau_BC(string pSolsx)
        {
            DataSet ds = new DataSet();
            ds = clsRp.DocMaMau_BC_EDC(pSolsx);
            chListMau.DataSource = ds.Tables[0].DefaultView;
            chListMau.DisplayMember = "TenMau_SP";
            chListMau.ValueMember = "MaMau_SP";
        }

         //đưa dữ liêu vào listbox Chi Tiết
        private void fDocChiTiet_BC(string pSolsx,string pMaMau)
        {
            DataSet ds = new DataSet();
            ds = clsRp.DocChiTiet_BC_EDC(pSolsx,pMaMau);
            chListCT.DataSource = ds.Tables[0].DefaultView;
            chListCT.DisplayMember = "TenChiTiet";
            chListCT.ValueMember = "MaChiTietMousse";
        }

        //đưa dữ liêu vào listbox Cở Số
        private void fDocCoSo_BC(string pSolsx, string pMaMau,string pMaCT)
        {
            DataSet ds = new DataSet();
            ds = clsRp.DocCoSo_BC_EDC(pSolsx, pMaMau, pMaCT);
            chListCoSo.DataSource = ds.Tables[0].DefaultView;
            chListCoSo.DisplayMember = "macoso";
            chListCoSo.ValueMember = "macoso";
        }

        private bool fKiemTraDuLieu(string strduongdan, string strbaocao)
        {
            FileInfo fileInfo = new FileInfo(strduongdan + strbaocao);
            if (fileInfo.Exists)
            {
                DataSet ds = new DataSet();
                clsRp.SoLSX = pSolsx;
                clsRp.Mamau = pMaMau;
                clsRp.Chitiet = pMaCT;
                clsRp.Coso = pCoSo;

                ds = clsRp.GetDataSet(strbaocao);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    return true;
                }
                else
                {
                    XtraMessageBox.Show("Không có dữ liệu hiển thị !", "Thông báo:");
                    return false;
                }
            }
            else
            {
                XtraMessageBox.Show("Đường dẫn report không tồn tại !", "Thông Báo:");
                return false;
            }
        }

        #endregion

        #region"Sự Kiện"

        private void FormLapKH_CLLH_Load(object sender, EventArgs e)
        {
            fKhoiTaoComBo();            
        }

        private void FormLapKH_CLLH_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Modifiers == Keys.Control)
                {
                    switch (e.KeyCode)
                    {
                        case Keys.P:
                            CmdIn_Click(sender, null);
                            return;
                    }
                }
                else if (e.KeyCode == Keys.Escape)
                {
                    this.Close();
                }

            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        // sự kiện chọn combobox
        private void cmbTuanKH_SelectedIndexChanged(object sender, EventArgs e)
        {
            TuanKH = cmbTuanKH.Text + cmbThangKH.Text + cmbNamKH.Text;
            fDocLSXTheoTuanKH_BC_EDC(TuanKH);
            fDocMau_BC("");
            fDocChiTiet_BC("", "");
            fDocCoSo_BC("", "", "");
            chkLSX.Checked = false;
            chkMau.Checked = false;
            chkCT.Checked = false; 
            chkCoSo.Checked = false;
        }

        private void chListLSX_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (flag == 1) return;
            pSolsx = "";

            for (int i = 0; i < chListLSX.ItemCount; i++)
            {
                if (chListLSX.GetItemChecked(i) == true)
                {
                    if (string.IsNullOrEmpty(pSolsx) == false)
                        pSolsx = pSolsx + "','";
                    pSolsx = pSolsx + chListLSX.GetItemValue(i);
                }
            }

            fDocMau_BC(pSolsx);
            fDocChiTiet_BC("", "");
            fDocCoSo_BC("", "", "");
            chkMau.Checked = false;
            chkCT.Checked = false;
            chkCoSo.Checked = false;
        }

        private void chListMau_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (flag == 1) return;
            pSolsx = "";

            for (int i = 0; i < chListLSX.ItemCount; i++)
            {
                if (chListLSX.GetItemChecked(i) == true)
                {
                    if (string.IsNullOrEmpty(pSolsx) == false)
                        pSolsx = pSolsx + "','";
                    pSolsx = pSolsx + chListLSX.GetItemValue(i);
                }
            }

            pMaMau = "";

            for (int i = 0; i < chListMau.ItemCount; i++)
            {
                if (chListMau.GetItemChecked(i) == true)
                {
                    if (string.IsNullOrEmpty(pMaMau) == false)
                        pMaMau = pMaMau + "','";
                    pMaMau = pMaMau + chListMau.GetItemValue(i);
                }
            }

            fDocChiTiet_BC(pSolsx, pMaMau);
        }

        private void chListCT_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (flag == 1) return;
            pSolsx = "";

            for (int i = 0; i < chListLSX.ItemCount; i++)
            {
                if (chListLSX.GetItemChecked(i) == true)
                {
                    if (string.IsNullOrEmpty(pSolsx) == false)
                        pSolsx = pSolsx + "','";
                    pSolsx = pSolsx + chListLSX.GetItemValue(i);
                }
            }

            pMaMau = "";

            for (int i = 0; i < chListMau.ItemCount; i++)
            {
                if (chListMau.GetItemChecked(i) == true)
                {
                    if (string.IsNullOrEmpty(pMaMau) == false)
                        pMaMau = pMaMau + "','";
                    pMaMau = pMaMau + chListMau.GetItemValue(i);
                }
            }

            pMaCT = "";

            for (int i = 0; i < chListCT.ItemCount; i++)
            {
                if (chListCT.GetItemChecked(i) == true)
                {
                    if (string.IsNullOrEmpty(pMaCT) == false)
                        pMaCT = pMaCT + "','";
                    pMaCT = pMaCT + chListCT.GetItemValue(i);
                }
            }

            fDocCoSo_BC(pSolsx, pMaMau, pMaCT);
        }
        
        private void chkLSX_CheckedChanged(object sender, EventArgs e)
        {
            chListLSX.BeginUpdate();
            try
            {
                flag = 1;
                for (int i = 0; i < chListLSX.ItemCount; i++)
                {
                    chListLSX.SetItemCheckState(i, chkLSX.CheckState);
                }
                pSolsx = "";

                for (int i = 0; i < chListLSX.ItemCount; i++)
                {
                    if (chListLSX.GetItemChecked(i) == true)
                    {
                        if (string.IsNullOrEmpty(pSolsx) == false)
                            pSolsx = pSolsx + "','";
                        pSolsx = pSolsx + chListLSX.GetItemValue(i);
                    }
                }

                fDocMau_BC(pSolsx);
                fDocChiTiet_BC("", "");
                fDocCoSo_BC("", "", "");
                chkMau.Checked = false;
                chkCT.Checked = false;
                chkCoSo.Checked = false;
                flag = 0;
            }
            finally
            {
                chListLSX.EndUpdate();
            }
        }

        private void chkMau_CheckedChanged(object sender, EventArgs e)
        {
            chListMau.BeginUpdate();
            try
            {
                flag = 1;
                for (int i = 0; i < chListMau.ItemCount; i++)
                {
                    chListMau.SetItemCheckState(i, chkMau.CheckState);
                }
                pSolsx = "";

                for (int i = 0; i < chListLSX.ItemCount; i++)
                {
                    if (chListLSX.GetItemChecked(i) == true)
                    {
                        if (string.IsNullOrEmpty(pSolsx) == false)
                            pSolsx = pSolsx + "','";
                        pSolsx = pSolsx + chListLSX.GetItemValue(i);
                    }
                }

                pMaMau = "";

                for (int i = 0; i < chListMau.ItemCount; i++)
                {
                    if (chListMau.GetItemChecked(i) == true)
                    {
                        if (string.IsNullOrEmpty(pMaMau) == false)
                            pMaMau = pMaMau + "','";
                        pMaMau = pMaMau + chListMau.GetItemValue(i);
                    }
                }

                fDocChiTiet_BC(pSolsx, pMaMau);
                flag = 0;
            }
            finally
            {
                chListMau.EndUpdate();
            }
        }

        private void chkCT_CheckedChanged(object sender, EventArgs e)
        {
            chListCT.BeginUpdate();
            try
            {
                flag = 1;
                for (int i = 0; i < chListCT.ItemCount; i++)
                {
                    chListCT.SetItemCheckState(i, chkCT.CheckState);
                }
                pSolsx = "";

                for (int i = 0; i < chListLSX.ItemCount; i++)
                {
                    if (chListLSX.GetItemChecked(i) == true)
                    {
                        if (string.IsNullOrEmpty(pSolsx) == false)
                            pSolsx = pSolsx + "','";
                        pSolsx = pSolsx + chListLSX.GetItemValue(i);
                    }
                }

                pMaMau = "";

                for (int i = 0; i < chListMau.ItemCount; i++)
                {
                    if (chListMau.GetItemChecked(i) == true)
                    {
                        if (string.IsNullOrEmpty(pMaMau) == false)
                            pMaMau = pMaMau + "','";
                        pMaMau = pMaMau + chListMau.GetItemValue(i);
                    }
                }

                pMaCT = "";

                for (int i = 0; i < chListCT.ItemCount; i++)
                {
                    if (chListCT.GetItemChecked(i) == true)
                    {
                        if (string.IsNullOrEmpty(pMaCT) == false)
                            pMaCT = pMaCT + "','";
                        pMaCT = pMaCT + chListCT.GetItemValue(i);
                    }
                }

                fDocCoSo_BC(pSolsx, pMaMau, pMaCT);
                flag = 0;
            }
            finally
            {
                chListCT.EndUpdate();
            }
        }

        private void chkCoSo_CheckedChanged(object sender, EventArgs e)
        {
            chListCoSo.BeginUpdate();
            try
            {
                for (int i = 0; i < chListCoSo.ItemCount; i++)
                {
                    chListCoSo.SetItemCheckState(i, chkCoSo.CheckState);
                }
            }
            finally
            {
                chListCoSo.EndUpdate();
            }
        }

        private void CmdIn_Click(object sender, EventArgs e)
        {
            pCoSo = "";

            for (int i = 0; i < chListCoSo.ItemCount; i++)
            {
                if (chListCoSo.GetItemChecked(i) == true)
                {
                    if (string.IsNullOrEmpty(pCoSo) == false)
                        pCoSo = pCoSo + "','";
                    pCoSo = pCoSo + chListCoSo.GetItemValue(i);
                }
            }

            if (fKiemTraDuLieu(strduongdan, strbaocao) == true)
            { 
                clsDungChung.pSoLSX = pSolsx;
                clsDungChung.pMaMau = pMaMau;
                clsDungChung.pMaCT = pMaCT;
                clsDungChung.pCoso = pCoSo;
                clsDungChung.strbaocao = strbaocao;

                formMain.pFormLapKH_EDC = null;
                this.Close();
            }
        }

        private void CmdThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        

       

      



       

       

       

        

       
       
        

       

        
    }
}
